package com.dst.note.note_edit;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TimePicker;

import com.dst.note.CustomPopWindow;
import com.dst.note.model.Options;
import com.dst.note.R;
import com.dst.note.editor.INoteEditorManager;
import com.dst.note.editor.NoteEditorSelectionListener;
import com.dst.note.utils.StringUtil;
import com.dst.note.utils.TimeUtil;

import java.util.Calendar;
import java.util.Date;

/**
 * Created By tgl on 2019/2/21
 */
public class EditNoteToolbar extends LinearLayout implements View.OnClickListener, NoteEditorSelectionListener {
    private ImageView mBackView;
    private ImageView mMoreView;
    private ImageView mVoiceBtn;
    private ImageView mRetrokeView;
    private ImageView mRecoverView;
    private ImageView mFormatView;
    private ImageView mPhotoView;
    private ImageView mEditSave;
    private ImageView mClockBtn;
    private EditNotePresenter mPresenter;
    private INoteEditorManager mStyleManager;

    public EditNoteToolbar(Context context) {
        this(context, null);
    }

    public EditNoteToolbar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EditNoteToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.edit_note_toolbar, this);
        mBackView = findViewById(R.id.back_btn);
        mBackView.setOnClickListener(this);

        mVoiceBtn = findViewById(R.id.voice_btn);
        mVoiceBtn.setOnClickListener(this);
        mMoreView = findViewById(R.id.more_btn);
        mMoreView.setOnClickListener(this);
        mMoreView.setVisibility(GONE);

        mEditSave = findViewById(R.id.save_btn);
        mEditSave.setOnClickListener(this);

        mRetrokeView = findViewById(R.id.retroke_btn);
        mRetrokeView.setEnabled(false);
        mRetrokeView.setOnClickListener(this);

        mRecoverView = findViewById(R.id.recover_btn);
        mRecoverView.setEnabled(false);
        mRecoverView.setOnClickListener(this);

        mPhotoView = findViewById(R.id.photo_btn);
        mPhotoView.setOnClickListener(this);

        mFormatView = findViewById(R.id.format_btn);
        mFormatView.setOnClickListener(this);

        mClockBtn = findViewById(R.id.clock_btn);
        mClockBtn.setOnClickListener(this);
    }

    public void setPresenter(EditNotePresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    public void writingMode() {
        mBackView.setVisibility(GONE);
        mEditSave.setVisibility(VISIBLE);
        mRecoverView.setVisibility(VISIBLE);
        mRetrokeView.setVisibility(VISIBLE);
        mPhotoView.setVisibility(VISIBLE);
        mFormatView.setVisibility(VISIBLE);
        mVoiceBtn.setVisibility(VISIBLE);
        mClockBtn.setClickable(true);
        showClock();
    }

    public void readingMode() {
        mBackView.setVisibility(VISIBLE);
        mEditSave.setVisibility(GONE);
        mRecoverView.setVisibility(GONE);
        mRetrokeView.setVisibility(GONE);
        mPhotoView.setVisibility(GONE);
        mFormatView.setVisibility(GONE);
        mVoiceBtn.setVisibility(GONE);
        mClockBtn.setClickable(false);
        showClock();
    }

    public void showClock() {
        if (mPresenter.getNote() == null) {
            return;
        }
        if (mPresenter.getMode() == EditNoteMode.Reading) {
            mClockBtn.setVisibility(mPresenter.getNote().isSetClock() ? VISIBLE : GONE);
        } else if (mPresenter.getMode() == null) {
            mClockBtn.setVisibility(GONE);
        } else if (mPresenter.getMode() == EditNoteMode.Writing) {
            mClockBtn.setVisibility(VISIBLE);
        }
        mClockBtn.setImageResource(mPresenter.getNote().isSetClock() ? R.drawable.ic_clock : R.drawable.ic_clock_gray);
        mClockBtn.setTag(mPresenter.getNote().isSetClock());
    }

    @Override
    public void onClick(View v) {
        checkPresenter();
        switch (v.getId()) {
            case R.id.back_btn:
                mPresenter.tapBack();
                break;
            case R.id.more_btn:
                mPresenter.tapMore();
                break;
            case R.id.voice_btn:
                mPresenter.voice();
                break;
            case R.id.save_btn:
                mPresenter.saveOrUpdate();
                break;
            case R.id.retroke_btn:
                mPresenter.tapRetroke(mStyleManager.commandRetroke());
                break;
            case R.id.recover_btn:
                mPresenter.tapRecover(mStyleManager.commandRecover());
                break;
            case R.id.format_btn:
                boolean isEnable = !mFormatView.isSelected();
                mPresenter.tapEditorBar(isEnable);
                break;
            case R.id.photo_btn:
                mPresenter.tapAttach();
                break;
            case R.id.clock_btn:
                boolean check = (boolean) mClockBtn.getTag();
                if (check) {
                    showDial(v);
                } else {
                    chooseTime();
                }

                break;
            default:
                break;
        }
    }

    private void checkPresenter() {
        if (mPresenter == null) {
            throw new RuntimeException("you need to call setPresenter() at first");
        }
    }

    public void setFormatEnable(boolean isEnable) {
        mFormatView.setSelected(isEnable);
    }

    public void setRetrokeEnable(boolean isEnable) {
        mRecoverView.setEnabled(isEnable);
    }

    public void setRecoverEnable(boolean isEnable) {
        mRetrokeView.setEnabled(isEnable);
    }

    @Override
    public void onStyleChange(int start, int end, Options options) {
        setRetrokeEnable(options.isCanRetroke());
        setRecoverEnable(options.isCanRecover());
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mStyleManager != null) {
            mStyleManager.removeSelectionChangeListener(this);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mStyleManager != null) {
            mStyleManager.addSelectionChangeListener(this);
        }
    }

    public void setStyleManager(INoteEditorManager editorManager) {
        this.mStyleManager = editorManager;
        this.mStyleManager.addSelectionChangeListener(this);
    }

    CustomPopWindow mCustomPopWindow;

    private void showDial(View view) {
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.window_clock, null);
        //创建并显示popWindow
        mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(getContext())
                .setView(contentView)
                .create()
                .showAsDropDown(view, 0, 20);
        //处理popWindow 显示内容
        OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                mCustomPopWindow.dissmiss();
                switch (v.getId()) {
                    case R.id.change_tv:
                        chooseTime();
                        break;
                    case R.id.clear_tv:
                        mPresenter.cancelAlarm();
                        showClock();
                        break;
                }
            }
        };
        contentView.findViewById(R.id.change_tv).setOnClickListener(listener);
        contentView.findViewById(R.id.clear_tv).setOnClickListener(listener);
        contentView.findViewById(R.id.cancel_tv).setOnClickListener(listener);


    }

    private void chooseTime() {
        final Calendar calendar = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, final int year, final int month, final int dayOfMonth) {
                TimePickerDialog timePickerDialog = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        String time = year + "-" + StringUtil.getLocalMonth(month) + "-" + StringUtil.getMultiNumber(dayOfMonth) + " " + StringUtil.getMultiNumber(hourOfDay) + ":" + StringUtil.getMultiNumber(minute);
                        addClock(TimeUtil.str2Date(time, TimeUtil.DEFAULT_TIME_FORMAT));
                    }
                }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), false);
                timePickerDialog.show();
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
        dialog.show();
    }

    private void addClock(Date date) {
        mPresenter.addClock(date == null ? 0 : date.getTime());
        showClock();
    }


}
